const { response } = require('express');
const express=require('express');
const pool=require('../pool.js');
//console.log(pool);
let router=express.Router();

//挂载路由
//1,商品列表 get /list
router.get('/list',(req,res)=>{
    //获取数据
    let obj=req.query;
    //console.log(obj);
    //检测数据是否为空
    if(!obj.pno) obj.pno=1;
    if(!obj.count) obj.count=2;
    //将当前页码和每页大小转为整型
    obj.count=parseInt(obj.count);
    obj.pno=parseInt(obj.pno);
    //console.log(obj);
    //计算start
    let start=(obj.pno-1)*obj.count;
    //执行SQL语句
    pool.query('SELECT * FROM xz_index_product LIMIT ?,?',[start,obj.count],(err,result)=>{
        if (err) throw err;
        res.send({
            code:200,
            msg:'ok',
            data:result
        });
    });
});
        
//2,商品详情 get /detail
router.get('/detail',(req,res)=>{
    //获取数据
    let obj=req.query;
    //console.log(obj);
    //验证数据是否为空
    if(!obj.lid){
        res.send({code:401,msg:'lid required'});
        return;
    }
    //将编号转为整型
    obj.lid=parseInt(obj.lid);
    //console.log(obj);
    //执行SQL语句
    pool.query('SELECT * FROM xz_laptop WHERE lid=?',[obj.lid],(err,result)=>{
        if (err) throw err;
        //console.log(result);
        if(result.length>0){
            res.send({
                code:200,
                msg:'ok',
                data:result[0]
            });
        }else{
            res.send({code:301,msg:'none exists'});
        }
    });
});

//3,删除商品 get /delete
router.get('/delete',(req,res)=>{
    //获取数据
    let obj=req.query;
    //console.log(obj);
    //验证数据,执行SQL语句
    if(!obj.lid){
        res.send({
            code:401,
            msg:'lid required'
        });
    }else{
        pool.query('DELETE FROM xz_laptop WHERE lid=?',[obj.lid],(err,result)=>{
                if (err) throw err;
                if(result.affectedRows>0){
                    res.send({
                        code:200,
                        msg:'delete success'
                    });
                }else{
                    res.send({
                        code:301,
                        msg:'delete err'
                    });
                }
            });
        }
});

//4,商品添加 post /add
router.post('/add',(req,res)=>{
    //获取数据
    let obj=req.body;
    //console.log(obj);
    obj.family_id=parseInt(obj.family_id);
    obj.title=String(obj.title);
    obj.subtitle=String(obj.subtitle);
    obj.price=parseFloat(obj.price);
    obj.promise=String(obj.promise);
    obj.spec=String(obj.spec);
    obj.lname=String(obj.lname);
    obj.os=String(obj.os);
    obj.memory=String(obj.memory);
    obj.resollution=String(obj.resollution);
    obj.video_card=String(obj.video_card);
    obj.cpu=String(obj.cpu);
    obj.video_memory=String(obj.video_memory);
    obj.category=String(obj.category);
    obj.disk=String(obj.disk);
    obj.details=String(obj.details);
    obj.shelf_time=parseInt(obj.shelf_time);
    obj.sold_count=parseInt(obj.sold_count);
    obj.is_onsale=parseInt(obj.is_onsale);
    //验证数据是否为空
    let i=400;
    for(let key in obj){
        i++;
        //console.log(key,obj[key]);//obj[key]
        if(!obj[key]){
            res.send({code:i,msg:key+' required'});
            return;
        }
    }
    //执行SQL语句
    pool.query('INSERT INTO xz_laptop SET ?',[obj],(err,result)=>{
        if (err) throw err;
        if(result.affectedRows>0){
            res.send({code:200,msg:'add success'});
        }else{
            res.send({code:500,msg:'add err'});
        }
    });
});

//导出路由器对象
module.exports=router;